<?php

namespace app\admin\controller;
use houdunwang\arr\Arr;
use think\Controller;
use think\Db;
use think\Request;
use app\common\model\Category as CategoryModel;
use think\Loader;

class Category extends Controller
{


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $data = Db::table('category')->select();
        $data =  Arr::tree($data,'cname','id','pid');
        return view('',compact('data'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::table('category')->select();
//        halt($data);die;
        $data =  Arr::tree($data,'cname','id','pid');
//        dump($data);die;
        return view('',compact('data'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(CategoryModel $category)
    {
        $data = input('post.');
        $validate = Loader::validate('Category');
        if(!$validate->check($data)){
            $this->error($validate->getError());die;
        }
        if (request()->isPost())
        {

            $res = $category->add($data);
            if ($res['valid']==1)
            {
                 return $this->success($res['message']);
            }else{
                 return $this->error($res['message']);
            }
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        return view();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        #查询栏目
        $data = Db::table('category')->select();
        #将所有栏目变成树状结构
        $data =  Arr::tree($data,'cname','id','pid');
        #查询栏目名称
        $old_C = Db::table('category')->find($id);

        return view('',compact('data','old_C','id'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(CategoryModel $category, $id)
    {
        $data = input('post.');
//        halt($data);
        $validate = Loader::validate('Category');
        if(!$validate->check($data))
        {
            $this->error($validate->getError());die;
        }
        if (request()->isPut())
        {
            $res = $category->edit($id,$data);
            if ($res['valid']==1)
            {
                return $this->success($res['message']);
            }else
            {
                return $this->error($res['message']);
            }
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
//        $data = Db::table('category')->select();
//        echo $data;
        $res = CategoryModel::destroy($id);
        if ($res)
        {
            return 1;
        }else
        {
            return 2;
        }
    }
}
